'''
Created on 2010-12-12

@author: Administrator
'''
import wx  
from wx.lib.wordwrap import wordwrap  
import wx.richtext as rt  
import os, sys  
  
ID_NEW   =  wx.NewId()  
ID_OPEN  =  wx.NewId()  
ID_SAVE  =  wx.NewId()  
ID_SAVEAS = wx.NewId()  
ID_EXIT  =  wx.NewId()  
ID_ABOUT =  wx.NewId()  
ID_CUT   =  wx.NewId()  
ID_COPY  =  wx.NewId()  
ID_PASTE =  wx.NewId()  
ID_SELECT = wx.NewId()  
ID_CLEAR =  wx.NewId()  
  
wildcard = "Python source (*.py)|*.py|"    
  
class SimpleEditor(wx.Frame):  
      
    cur_file = ''  # current operate file  
      
    def __init__(self, parent, id, title):  
        wx.Frame.__init__(self, parent, id, title, size=(1024, 768))  
          
        #------------create text area--------------  
        edit_area = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE)  
        self.text = edit_area  
        wx.CallAfter(self.text.SetFocus)  
          
        #------------create status bar-------------  
        self.CreateStatusBar()  
        self.SetStatusText('Here display operate status.')  
        #------------create menubar----------------  
        menubar = wx.MenuBar(wx.MB_DOCKABLE)  
        #------------create menus------------------  
        file = wx.Menu()  
        edit = wx.Menu()  
        help = wx.Menu()  
          
        file.Append(ID_NEW, '&New\tCtrl+N', 'Create a new File')  
        file.Append(ID_OPEN, '&Open\tCtrl+O', 'Open a File')  
        file.Append(ID_SAVE, '&Save\tCtrl+S', 'Save a File')  
        file.Append(ID_SAVEAS, '&Save As\tShift+Ctrl+S', 'Save as a File')  
        file.AppendSeparator()  
        file.Append(ID_EXIT, 'E&xit\tCtrl+W', 'Exit Current window')  
          
        edit.Append(ID_CUT, '&Cut\tCtrl+X', 'Cut select text')  
        edit.Append(ID_COPY, '&Copy\tCtrl+C', 'Copy select text')  
        edit.Append(ID_PASTE, '&Paste\tCtrl+V', 'Paste clipboard text')  
        edit.AppendSeparator()  
        edit.Append(ID_SELECT, '&Select All\tCtrl+A', 'Select all')  
        edit.Append(ID_CLEAR, '&Clear\tCtrl+D', 'delete select text')  
  
        help.Append(ID_ABOUT, '&About Simple Editor', 'Simple Editor Message')  
        menubar.Append(file, '&File')  
        menubar.Append(edit, '&Edit')  
        menubar.Append(help, '&Help')  
        self.Bind(wx.EVT_MENU, self.OnOpen, id=ID_OPEN)  
        self.Bind(wx.EVT_MENU, self.OnSave, id=ID_SAVE)  
        self.Bind(wx.EVT_MENU, self.OnSaveAs, id=ID_SAVEAS)  
        self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)  
        self.Bind(wx.EVT_MENU, self.OnAbout, id=ID_ABOUT)  
        self.Bind(wx.EVT_MENU, self.OnCut, id=ID_CUT)  
        self.Bind(wx.EVT_MENU, self.OnCopy, id=ID_COPY)  
        self.Bind(wx.EVT_MENU, self.OnSelectAll, id=ID_SELECT)  
        self.Bind(wx.EVT_MENU, self.OnClear, id=ID_CLEAR)  
        self.Bind(wx.EVT_MENU, self.OnPaste, id=ID_PASTE)  
          
        # set the menu bar  
        self.SetMenuBar(menubar)     
        #-------------------create tools bar-----------------------  
        toolbar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER  
                                 | wx.TB_FLAT | wx.TB_TEXT)  
        toolbar.AddLabelTool(ID_NEW, '', wx.Bitmap('icons/new.gif'))  
        toolbar.AddLabelTool(ID_OPEN, '', wx.Bitmap('icons/open.gif'))  
        toolbar.AddLabelTool(ID_SAVE, '', wx.Bitmap('icons/save.gif'))  
        
    #-------------------File menu event-------------------------  
    def OnOpen(self, event):  
        dlg = wx.FileDialog(  
            self, message="Choose a file", defaultDir=os.getcwd(),  
            defaultFile="", wildcard="All files (*.*)|*.*",  
            style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR  
            )  
        if dlg.ShowModal() == wx.ID_OK:  
            #paths = dlg.GetPaths()  
            #print 'You selected files is %s:' % (paths)  
            path = dlg.GetPath()  
            if path: # open a file  
                self.cur_file = path  
                #self.text.SetValue(open(path,'r').read())  
                self.text.LoadFile(path, rt.RICHTEXT_TYPE_TEXT)  
        dlg.Destroy()  
          
    def OnSave(self, event):  
        cur_content = self.text.GetValue()  
        if not self.cur_file:  
            # if current file is new  
            dlg = wx.FileDialog(  
                self, message="Save file as ...", defaultDir=os.getcwd(),  
                defaultFile="", wildcard="*.*", style=wx.SAVE  
                )  
            if dlg.ShowModal() == wx.ID_OK:  
                self.cur_file = dlg.GetPath()  
            else: pass         
        if self.cur_file:  
            fp = open(self.cur_file, 'w')  
            fp.write(cur_content)  
            fp.close()  
          
    def OnSaveAs(self, event):  
        dlg = wx.FileDialog(  
            self, "Choose a filename", wildcard="All files (*.*)|*.*",  
            style=wx.SAVE)  
        if dlg.ShowModal() == wx.ID_OK:  
            path = dlg.GetPath()  
            if path:  
                #self.text.SaveFile(path)  
                self.cur_file = path  
                fp = open(path, 'w')  
                fp.write(self.text.GetValue())  
                fp.close()  
        dlg.Destroy()  
              
    def OnExit(self, event):  
        self.Close(True)  
    #-------------------Edit menu event---------------------------  
    def OnCut(self, event):  
        # Cut select text  
        self.text.Cut()  
          
    def OnCopy(self, event):  
        # copy select text  
        self.text.Copy()  
          
    def OnPaste(self, event):  
        # paste clipboard text  
        self.text.Paste()  
                
    def OnSelectAll(self, event):  
        # select all text  
        self.text.SelectAll()  
          
    def OnClear(self, event):  
        # delete select text  
        # self.text.Clear() is delete all text  
        start,end = self.text.GetSelection()  
        self.text.Remove(start, end)     
          
    #-------------------Help menu event---------------------------  
    def OnAbout(self, event):  
        # First we create and fill the info object  
        info = wx.AboutDialogInfo()  
        info.Name = "Simple Editor"  
        info.Version = "beta 1.0"  
        info.Copyright = "(C) 2008 Programmers and Coders Everywhere"  
        # copy from wxPython demo  
        info.Description = wordwrap(  
            "A \"hello world\" program is a software program that prints out "  
            "\"Hello world!\" on a display device. It is used in many introductory "  
            "tutorials for teaching a programming language."  
              
            "\n\nSuch a program is typically one of the simplest programs possible "  
            "in a computer language. A \"hello world\" program can be a useful "  
            "sanity test to make sure that a language's compiler, development "  
            "environment, and run-time environment are correctly installed.",  
            350, wx.ClientDC(self))  
        info.WebSite = ("http://www.bobo.com.cn", "BOBO VIDEO")  
        info.Developers = ["Purpen"]  
        # Then we call wx.AboutBox giving it that info object  
        wx.AboutBox(info)  
      
class RunApp(wx.App):  
    def OnInit(self):  
        win = SimpleEditor(None, 1, 'SimpleEditor')  
        self.SetTopWindow(win)  
        win.Show(True)  
        return True  # can't forget  
          
if __name__ == '__main__':  
    RunApp().MainLoop()